//
//  StatusCell.swift
//  Weibo T
//
//  Created by mac on 15/12/1.
//  Copyright © 2015年 陈肖坤. All rights reserved.
//

import UIKit
import SnapKit

let StatusCellMargin: CGFloat = 12
let StatusCellImageWidth: CGFloat = 35

class StatusCell: UITableViewCell {
    //底部约束
    var topConstraints: Constraint?
    
    //添加模型属性
    var status: Status? {
        didSet {
//            print(status?.retweeted_status?.text)
            qriginalView.status = status
            //移除顶部工具条的约束
            topConstraints?.uninstall()
            
            //设置转发微博的数据模型 
            if status?.retweeted_status != nil {
                //就是转发微博
                retweetedView.retweetedStatus = status?.retweeted_status
                //显示转发微博
                retweetedView.hidden = false
                //更新约束
                bottomView.snp_updateConstraints(closure: { (make) -> Void in
                    self.topConstraints = make.top.equalTo(retweetedView.snp_bottom).constraint
                })
            }else {
            
                //隐藏转发微博试图
                retweetedView.hidden = true
                bottomView.snp_updateConstraints(closure: { (make) -> Void in
                    self.topConstraints = make.top.equalTo(qriginalView.snp_bottom).constraint
                })
            }
        }
    }

    //重写init方法
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
        selectionStyle = .None
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //MARK: 设置UI
    private func setupUI() {
        contentView.addSubview(qriginalView)
        contentView.addSubview(retweetedView)
        contentView.addSubview(bottomView)
        //设置布局
        qriginalView.snp_makeConstraints { (make) -> Void in
            make.top.left.right.equalTo(contentView)
        }
        retweetedView.snp_makeConstraints { (make) -> Void in
            make.left.right.equalTo(contentView)
            make.top.equalTo(qriginalView.snp_bottom)
        }
        bottomView.snp_makeConstraints { (make) -> Void in
           self.topConstraints = make.top.equalTo(retweetedView.snp_bottom).constraint
            make.left.right.equalTo(contentView)
            make.height.equalTo(40)
        }
        
        contentView.snp_makeConstraints { (make) -> Void in
            make.top.left.right.equalTo(self)
            //底部约束
            make.bottom.equalTo(bottomView.snp_bottom)
        }
    }
    
    //MARK: 懒加载子试图
    //头部
    private lazy var qriginalView: StatusQriginalView = StatusQriginalView()
    
    private lazy var retweetedView: StatusRetweetedView  = StatusRetweetedView()

    //底部
    private lazy var bottomView: StatusBottomView = StatusBottomView()

}
